#!/usr/bin/perl
#mdm-search-lib.pl

do "../web-lib.pl";
&init_config();
%access = &get_module_acl();
&foreign_require("mdmhistory","mdmhistory-lib.pl");

sub list_servers
{
	local ($f, @rv);
	opendir(DIR, $config{'searchdir'});
	while($f = readdir(DIR)) {
		if ($f =~ /^(\S+)\.serv$/) {
			push(@rv, &get_server($1));
			}
		}
	closedir(DIR);
	@rv = sort {$a->{'id'} cmp $b->{'id'} } @rv;
	return @rv;
}

# get_server(id)
sub get_server
{
	local $serv;
	$serv->{'id'} = $_[0];
	&read_file("$config{'searchdir'}/$_[0].serv", $serv) || return undef;
	return $serv;
}

sub list_controlledservs
{
	local ($f, @rv);
	opendir(DIR, $config{'searchdir'});
	while($f = readdir(DIR)) {
		if ($f =~ /^(\S+)\.serv$/) {
			local $device=&get_controlledserver($1);
			if ($device ne undef){	
				push(@rv,$device);
			}
		}
	}
	closedir(DIR);
	@rv = sort {$a->{'id'} cmp $b->{'id'} } @rv;
	return @rv;
}

# get_controlledserver(id)
sub get_controlledserver
{
	local $serv;
	&read_file("$config{'searchdir'}/$_[0].serv", $serv) || return undef;
	if ($serv->{'user'} =~ /\S/){
		return $serv;
	}else{
		return undef;
	}
}

#save_new_serv(systemid,hostname,fromip)
sub save_new_serv() 
{
	$filename=$config{'searchdir'}.$_[0].".serv";
	$file[0]="port=10000\n";
	$file[1]="ssl=1\n";
	$file[2]="id=$_[0]\n";
	$file[3]="hostname=$_[1]\n";
	$file[4]="ip=$_[2]\n";
	$file[5]="user=\n";
	$file[6]="passwd=\n";
	$file[7]="group=\n";
	$file[8]="status=$text{'meg_devrun'}\n";

	open(FILE,">$filename");
	#open(FILE,">test.search");
	print FILE @file;
	close(FILE);		
}

#save_old_serv(systemid,hostname,fromip,user,passwd)
#if user and passwd needn't to be changed, set $user=## and $passwd=##
sub save_old_serv(){
	local $filename=$config{'searchdir'}.$_[0].".serv";
	open(FILE,$filename); #||&error("open failed: $!");
	local @file=<FILE>;
	close(FILE);

	for ($i=0;$i<@file;$i++){
		if ($file[$i] =~ /^hostname=/){
			$file[$i] = "hostname=$_[1]\n";
		}elsif($file[$i] =~ /^ip=/){
			$file[$i] = "ip=$_[2]\n";
		}elsif($file[$i] =~ /^user=/){
			if (($_[3] ne '##') || ($_[4] ne '##')){
				$file[$i] = "user=$_[3]\n";
			}
		}elsif($file[$i] =~ /^passwd=/){
			if (($_[3] ne '##') || ($_[4] ne '##')){
				$file[$i] = "passwd=$_[4]\n";
			}
		}elsif($file[$i] =~ /^status=/){
			if (($_[3] eq "##") && ($_[4] eq "##")){
				$file[$i] = "status=$text{'meg_devrun'}\n";
			}
		}
	}

	open(FILE,">$filename");
	#open(FILE,">test.search");
	print FILE @file;
	close(FILE);
}

#set_status_norun($systemid)
sub set_status_unrun()
{
	local $filename=$config{'searchdir'}.$_[0].".serv";
	open(FILE,$filename);
	local @file=<FILE>;
	close(FILE);
	
	for($i=0;$i<@file;$i++){
		if ($file[$i] =~ /^status=/){
			$file[$i]="status=\n";
		}
	}
	
	open(FILE,">$filename");
	print FILE @file;
	close(FILE);	
}


#del_serv(systemid)
sub del_serv()
{
	local $filename=$config{'searchdir'}.$_[0].".serv";
	unlink($filename);	
}

#check if the user and password is ok
#sure_user(systemid)
sub sure_user()
{
	local $ifsure=&remote_foreign_ping($_[0]);
	return $ifsure;
}

sub inst_error
{
	local $inst_error_msg = join("", @_);
}

1;
